using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using StupidSpaceThing.MenuSystem;
using Microsoft.Xna.Framework.Graphics;

namespace StupidSpaceThing
{
	internal static partial class LevelSystem
	{
		private static JellyFish level3_jellyfish;
		public static Faction Level3_faction1;
		public static Faction Level3_faction2;
		public static Faction Level3_faction3;
		public static Faction Level3_faction4;
		private static bool level3_completed;

		private static void level3() {
			level3_completed = false;
			Faction.None.Add(new TradePortal(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, 20), (float)(Math.PI / 2), 100, 1));
			Faction.None.Add(new TradePortal(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, Game1.CurMap.Height * Game1.CurMap.CellSize - 20), (float)(Math.PI / 2), 100, 1));
			Faction.None.Add(new TradePortal(new Vector3(20, 0, Game1.CurMap.Height * Game1.CurMap.CellSize / 2), (float)(Math.PI / 2), 100, 1));
			Faction.None.Add(new TradePortal(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize - 20, 0, Game1.CurMap.Height * Game1.CurMap.CellSize / 2), (float)(Math.PI / 2), 100, 1));

			new Blue(Game1.CurMap.TopLeft + new Vector3(-30, 0, -30));
			new Yellow(Game1.CurMap.TopRight + new Vector3(30, 0, -30));
			new Red(Game1.CurMap.BottomRight + new Vector3(30, 0, 30));
			new Green(Game1.CurMap.BottomLeft + new Vector3(-30, 0, 30));

			// turret defenses
			int totalTurrets = 6;
			float interval = (float)(Math.PI * 2 / (totalTurrets - 1));
			foreach( Faction faction in Faction.Factions ){
				if( faction == Faction.None ) continue;
				for( int i = 0; i < totalTurrets; i++ ) {
					Turret turret = new Turret(new Vector3(faction.Hive.Position.X + ((float)Math.Cos(i * interval)) * 30, 0, faction.Hive.Position.Z + ((float)Math.Sin(i * interval)) * 30));
					turret.Entity.Armor = 30;
					faction.Add(turret);
				}
			}

			Map.Cell freeCell = null;

			Faction[] factionsArray = { Faction.Green, Faction.Yellow, Faction.Red, Faction.Blue };
			List<Faction> factions = new List<Faction>(factionsArray);
			Level3_faction1 = factions[Game1.Random.Next(0, factions.Count)]; factions.Remove(Level3_faction1);
			Level3_faction2 = factions[Game1.Random.Next(0, factions.Count)]; factions.Remove(Level3_faction2);
			Level3_faction3 = factions[Game1.Random.Next(0, factions.Count)]; factions.Remove(Level3_faction3);
			Level3_faction4 = factions[Game1.Random.Next(0, factions.Count)]; factions.Remove(Level3_faction4);

			// add info pylons
			level3_addPylon(Level3_faction1, new Level3_PuzzleGuide(Game1.SpriteFont, 1, Level3_faction4, Level3_faction2));
			level3_addPylon(Level3_faction2, new Level3_PuzzleGuide(Game1.SpriteFont, 1, Level3_faction3, Level3_faction4));
			level3_addPylon(Level3_faction3, new Level3_PuzzleGuide(Game1.SpriteFont, 1, Level3_faction2, Level3_faction1));
			level3_addPylon(Level3_faction4, new Level3_PuzzleGuide(Game1.SpriteFont, 1, Level3_faction1, Level3_faction3));			

			// no turret production
			Turret._ProductionCost = 1000000;
			Scorpion._ProductionTime = 3000;
			Scorpion._ProductionCost = 1200;
			DataTrain._ProductionCost = 400;

			level3_jellyfish = new JellyFish(Game1.CurMap.Center);
			Faction.None.Add(level3_jellyfish);

			// mines
			/*for( int i = 0; i < 30; i++ ) {
				Faction mineFaction = Faction.RandomFaction();
				freeCell = Game1.CurMap.GetFreeCell(level3_jellyfish.Position, 20);
				Mine mine = new Mine(new Vector3(freeCell.X * Game1.CurMap.CellSize, 0, freeCell.Z * Game1.CurMap.CellSize));
				mine.DamageMultiplier = 2;
				mineFaction.Add(mine);
			}*/

						
			freeCell = Game1.CurMap.GetFreeCell(level3_jellyfish.Position, 10);
			Game1.Player = new Player(new Vector3(freeCell.X * Game1.CurMap.CellSize, Map.MaxHeight + 0.2f, freeCell.Z * Game1.CurMap.CellSize));
			Game1.Player.Entity.Armor = 2;
			Game1.Player.CanChangeFaction = true;
			Game1.Player.CanControlMines = true;
			Game1.Player.CanShoot = false;
			
			Faction.None.Add(Game1.Player);
		}

		private static void level3_addPylon(Faction faction, Level3_PuzzleGuide guide) {
			Map.Cell freeCell = null;
			Pylon pylon = null;
			freeCell = Game1.CurMap.GetFreeCell(faction.Hive.Position, 6);
			pylon = new Pylon(new Vector3(freeCell.X * Game1.CurMap.CellSize, 0, freeCell.Z * Game1.CurMap.CellSize));
			pylon.Moving = false;
			pylon.InfoText = guide;
			faction.Add(pylon);
		}

		public static void Level3_StartJellyFish() {
			level3_jellyfish.Shooting = true;
			GuideSystem.ClosePermanent();			
		}

		private static void level3_Update() {
			// update jelly
			if( level3_jellyfish.Faction != Game1.Player.Faction ) {
				level3_jellyfish.Faction.Remove(level3_jellyfish);
				Game1.Player.Faction.Add(level3_jellyfish);
			}
			// check if all alive
			if( level3_completed ) return;
			foreach( Faction faction in Faction.Factions ) {
				if( faction == Faction.None ) continue;
				if( faction.Defeated ) {
					level3_completed = true;
					if( faction == Level3_faction2 && level3_jellyfish.Faction == Level3_faction4 ) {
						GuideSystem.ShowPriority(new TextGuide("You have served us well Man-in-the-middle. Good bye for now", Game1.SpriteFont, TextAlign.Both, 1, Color.White, 5000, delegate() {
							Game1.Level = 1;
							TopMenu.Menu.StopGame();
						}), true);
					} else {
						GuideSystem.ShowPriority(new TextGuide("You got it wrong! This will cost us dearly ...", Game1.SpriteFont, TextAlign.Both, 1, Color.White, 5000, delegate() {
							Game1.Game.Reset();
						}), true);
					}
				}
			}
		}
	}
}
